package com.haoshuang.zhouzhoubang.ui.page.order.rework.rework.personal

import android.app.Activity
import android.content.Context
import android.os.Bundle
import com.haoshuang.zhouzhoubang.databinding.ActivityPersonalReworkBinding
import com.haoshuang.zhouzhoubang.ui.dialog.ConfirmDialog
import com.haoshuang.zhouzhoubang.ui.page.order.rework.rework.compensate.CompensateActivity
import com.haoshuang.zhouzhoubang.ui.page.order.time.UpdateServiceTimeActivity
import com.lxj.xpopup.XPopup
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.callPhone
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.startPageForResult
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger

/**
 *  个人订单返工
 */
class PersonalReworkActivity : BaseVMActivity<PersonalReworkViewModel, ActivityPersonalReworkBinding>() {

    companion object {
        fun launch(context: Context){
            context.startPage<PersonalReworkActivity>()
        }
    }

    private val orderId by lazy { intent.getStringExtra("order_id")?:"" }

    // 客户电话
    private var user_phone = ""
    private var cate_id = ""
    var service_day = ""
    var service_time = ""

    override fun initView(savedInstanceState: Bundle?) {
        binding.titleBar.title = "返工处理"
        binding.titleBar.leftImageView().click { finish() }

        binding.callPhone.clickWithTrigger {
            if (user_phone.isBlank()) {
                toast("电话不能为空")
                return@clickWithTrigger
            }
            callPhone(user_phone)
        }

        binding.tvTime2.clickWithTrigger {

            startPageForResult<UpdateServiceTimeActivity>("cate_id" to cate_id){code, data ->
                if (code == Activity.RESULT_OK){
                    if (data != null) {
                        service_day = data.getStringExtra("service_day").toString()
                        val service_day1 = data.getStringExtra("service_day1")
                        service_time = data.getStringExtra("service_time").toString()
                        binding.tvTime.text = "$service_day1     $service_time"
                    }
                }
            }
        }

        binding.tvRework.clickWithTrigger {
            if (binding.tvTime.text.toString().isEmpty()){
                XPopup.Builder(this)
                    .asCustom(
                        ConfirmDialog(
                            context = this,
                            title = "温馨提示",
                            content = "请选择与客户协商的返工上门时间",
                            confirmStr = "我知道了",
                            isHideCancel = true,
                            onCancel = {},
                            onConfirm = {})
                    )
                    .show()
                return@clickWithTrigger
            }

            vm.masterAgreeRework(
                orderId,
                service_day,
                service_time
            )

        }

        binding.llCompensate.clickWithTrigger {
            startPageForResult<CompensateActivity>("order_id" to orderId){ code, data ->
                if (code == 200){
                    setResult(200)
                    finish()
                }
            }
        }
    }

    override fun initData() {
        vm.confirmOrderTime(orderId)
    }

    override fun startObserve() {

        vm.confirmOrderTimeData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                binding.apply {
                    realName.text = it?.data?.first_info?.real_name
                    storeName.text = it?.data?.first_info?.store_name
                    serviceTime.text = it?.data?.first_info?.service_time
                    baseAddress.text = it?.data?.first_info?.base_address
                    user_phone = it?.data?.first_info?.user_real_phone.toString()
                    cate_id = it?.data?.first_info?.cate_id.toString()
                }
            }
        }
    }

}